<template>
    <div>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="系统名称" prop="systemName">
        <el-input v-model="formData.systemName" placeholder="请输入系统名称" style="width: 600px;"/>
      </el-form-item>
      <el-form-item label="系统Logo" prop="systemLogo">
        <UploadImg v-model="formData.systemLogo" :fileType="['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml']"/>
      </el-form-item>
      <el-form-item label="登录页背景图" prop="loginBgPic">
        <UploadImg v-model="formData.loginBgPic" :fileType="['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml']"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { BaseConfigApi, BaseConfigVO } from '@/api/system/baseconfig'

/** 基础配置 表单 */
defineOptions({ name: 'BaseConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  systemName: undefined,
  systemLogo: undefined,
  loginBgPic: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

const open = async () => {
  formLoading.value = true
  try {
    formData.value = await BaseConfigApi.getBaseConfig()
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
open()

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseConfigVO
    await BaseConfigApi.saveBaseConfig(data)
      message.success('保存成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    systemName: undefined,
    systemLogo: undefined,
    loginBgPic: undefined
  }
  formRef.value?.resetFields()
}
</script>